package com.zjs.jsp.fdservice.service;

import com.currencyboot.service.rocketmq.MqProducer;
import com.zjs.jsp.domain.config.Param;
import com.zjs.jsp.domain.jsp.Jsp_Order;
import com.zjs.jsp.domain.jsp.Jsp_Task;
import com.zjs.jsp.fdservice.Help.OnlyDataHelp;
import com.zjs.jsp.fdservice.service.mongodb.ExceptionOrderExpand;
import com.zjs.jsp.fdservice.service.mongodb.JspTaskRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * 新建任务包
 *
 * @author 李文
 * @create 2017-09-01 18:48
 **/
@Component
public class NewJspTask
{
    private static final OnlyDataHelp ONLY_DATA_HELP = new OnlyDataHelp();

    private static final Logger LOGGER = LoggerFactory.getLogger(NewJspTask.class);

    @Autowired
    private MqProducer mqProducer;

    @Value("${Topic}")
    private String Topic;

    /**
     * 订单接受成功
     */
    @Value("${Tags}")
    private String TagsCD;


    @Autowired
    private ExceptionOrderExpand exoExpand;
    @Autowired
    private JspTaskRepository jspTaskRepository;

    /**
     * 创建一个新的任务包
     *
     * @param jspOrder
     * @param pk
     * @return 是否 新建任务包成功
     */
    public Boolean CreateNewJspTask(Jsp_Order jspOrder, String pk, Param p, String strBody) {
        return CreateNewJspTask(jspOrder, pk, p, strBody, 0);
    }


    /**
     * 创建一个新的任务包 人工分单
     *
     * @param jspOrder
     * @param pk
     * @return 是否 新建任务包成功
     */
    public Boolean CreateNewJspTaskRG(Jsp_Order jspOrder, String pk, Param p, String strBody) {
        return CreateNewJspTask(jspOrder, pk, p, strBody, 1);
    }

    /**
     * 创建一个新的任务包
     *
     * @param jspOrder
     * @param pk
     * @return 是否 新建任务包成功
     */
    public Boolean CreateNewJspTask(Jsp_Order jspOrder, String pk, Param p, String strBody, Integer fdlx) {
        Jsp_Task jspTask = new Jsp_Task();
        try {
            jspTask.setTk(getFixLenthString(11));
            jspTask.setPk(pk);
            jspTask.setSum(1);
            jspTask.setDdjs(p.getDDZHSX());
            jspTask.setDate(new Date());
            List<Jsp_Order> JspOrders = new ArrayList<>();
            JspOrders.add(jspOrder);
            jspTask.setOd(JspOrders);
            jspTask.setZd(p.getRWZDDD());
            //生成一个任务包唯一编码
            String wy = ONLY_DATA_HELP.getWyString(JspOrders);
            jspTask.setWy(wy);
            jspTask.setXfzt(0);
            jspTask.setJs(p.getJDSX());
            jspTask.setCzzt(10);
            jspTask.setMdbm(jspOrder.getMdbm());
            jspTask.setClientFlag(jspOrder.getClientFlag());
            jspTask.setFdlx(fdlx);
            //发送订单接受成功MQ
            mqProducer.send(Topic, TagsCD, "{\"taskCode\":null,\"orderCode\":\"" + jspOrder.getPoc() + "\",\"status\":\"00\",\"excpCode\":\"\",\"excpNote\":\"\"}");
            jspTaskRepository.save(jspTask);
            LOGGER.info(" {} 创建任务成功",jspOrder.getOuuid());
        } catch (Exception e) {
            exoExpand.saveEx(jspOrder, e, " 创建新的任务包保存数据 数据是： " + strBody, 3, "创建新的任务包保存数据 异常 " + e.getMessage());
            return false;
        }
        return true;
    }

    private String getFixLenthString(int strLength) {
        //Random rm = new Random();
        //// 获得随机数
        //double pross = (1 + rm.nextDouble()) * Math.pow(10, strLength);
        //// 将获得的获得随机数转化为字符串
        //String fixLenthString = String.valueOf(pross);
        //// 返回固定的长度的随机数
        //return fixLenthString.substring(2, strLength + 1);

        return UUID.randomUUID().toString().replace("-", "");

    }
}
